package cn.sangedon.review.analysis.utils;

import cn.sangedon.review.common.domain.analysis.StockStrategy;
import cn.sangedon.review.common.domain.data.SnowballQuotation;
import cn.sangedon.review.common.domain.data.StockDayInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * 校验工具类
 */
@Slf4j
public class CheckUtil {
    /**
     * 校验策略
     *
     * @param stockStrategy
     */
    public static void checkParam(StockStrategy stockStrategy) {
        if (stockStrategy == null) {
            log.info("待更新策略不能为空");
            throw new RuntimeException("待更新策略不能为空");
        }
        if (!StringUtils.hasText(stockStrategy.getStrategyName())) {
            log.info("策略名称不能为空");
            throw new RuntimeException("策略名称不能为空");
        }
    }

    /**
     * 下跌缩量红的策略校验
     *
     * @param today 今日交易数据
     * @param yestoday 昨日交易数据
     * @return true:符合策略要求
     */
    public static boolean fallShrinkRed(StockDayInfo today, StockDayInfo yestoday) {
        if (yestoday.getChangeRate().signum() > 0) {
            return false;
        }

        if (today.getChangeRate().signum() != 1) {
            return false;
        }

        if (today.getTradingVolume().compareTo(yestoday.getTradingVolume()) > 0) {
            return false;
        }

        return true;
    }

    /**
     * 下跌缩量红的策略校验
     *
     * @param today 今日交易数据
     * @param yestoday 昨日交易数据
     * @return true:符合策略要求
     */
    public static boolean fallShrinkRed(SnowballQuotation today, StockDayInfo yestoday) {
        if (yestoday.getChangeRate().signum() > 0) {
            return false;
        }

        if (today.getChangeRate().signum() != 1) {
            return false;
        }

        if (today.getTradingVolume().compareTo(yestoday.getTradingVolume()) > 0) {
            return false;
        }

        return true;
    }

    /**
     * 上涨缩量红的策略校验
     *
     * @param today 今日交易数据
     * @param yestoday 昨日交易数据
     * @return true:符合策略要求
     */
    public static boolean riseShrinkRed(StockDayInfo today, StockDayInfo yestoday) {
        if (yestoday.getChangeRate().signum() != 1) {
            return false;
        }

        if (today.getChangeRate().signum() != 1) {
            return false;
        }

        if (today.getTradingVolume().compareTo(yestoday.getTradingVolume()) > 0) {
            return false;
        }

        return true;
    }
}
